// pages/classify/classify.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    allList: [{
      id:0,
      name: "数码",
      list: [{
        id:0,
        name: "二级1"
      },{
        id:1,
        name: "二级2"
      }]
    },{
      id:1,
      name: "家具",
      list: [{
        id:0,
        name: "二级3"
      },{
        id:1,
        name: "二级4"
      },{
        id:3,
        name: "二级5"
      },{
        id:4,
        name: "二级6"
      }]
    }],
    firstLevel: 0,
    secList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '分类',
    })

    this.getClassifyList();
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  // 请求一级分类和二级分类
  getClassifyList() {
    let that = this;
    // ajax.ajax(api.ClassifyList, {})
    //   .then((val) => {
    //     console.log(val)
    //     if (val.Tag == 1 && val.Result) {
    //       that.setData({
    //         allList: val.Result.list
    //       })
    //       that.checkoutClassifyList();
    //     } else {
    //       wx.showToast({title: "加载失败，请重试", icon: 'none'})
    //     }
    //   })
    //   .catch((val) => {
    //     wx.showToast({title: "加载失败，请重试", icon: 'none'})
    //   })
  },

  // 选择一级分类
  chooseFirstLevel (e) {
    let that = this;
    let index = that.data.firstLevel;
    let targetIndex = e.currentTarget.dataset.index;
    if (index != targetIndex) {
      that.setData({
        firstLevel: targetIndex
      })
      that.checkoutClassifyList();
    }
  },

  // 展示分类列表
  checkoutClassifyList() {
    let that = this;
    let index = that.data.firstLevel;
    let secList = that.data.allList[index].list;
    that.setData({
      secList
    })
  },

  // 选择二级分类项
  chooseSecondLevel (e) {
    let that = this;
    let targetIndex = e.currentTarget.dataset.index;
    let target = that.data.secList[targetIndex];
    console.log(target)
  }

})